/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BASearchBar.m
//  redia-bapps-ios
//
//  Created by Nikolaj Tørring on 8/9/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BASearchBar.h"
#import <QuartzCore/QuartzCore.h>
#import "BAUtils.h"
#import "BALibrarySettings.h"


@implementation BASearchBar

+(void)initialize
{
    [[UITextField appearanceWhenContainedIn:[BASearchBar class], nil] setFont:BA_FONT_REGULAR(12)];
    [[UITextField appearanceWhenContainedIn:[BASearchBar class], nil] setTextColor:[UIColor colorWithWhite:102.0/255.0 alpha:1]];
}

-(void)commonInitBASearchBar
{
}

- (id)init
{
    self = [super init];
    if (self) {
        [self commonInitBASearchBar];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInitBASearchBar];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self commonInitBASearchBar];
    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];
    
    
    [self setTintColor:[UIColor clearColor]];
    
    
    UIImage* emptyimage = [[UIImage alloc] init];
    [self setBackgroundImage:emptyimage];
    
    [self setSearchFieldBackgroundImage:[[UIImage imageNamed:@"searchBarTextField"] resizableImageWithCapInsets:UIEdgeInsetsMake(4, 4, 4, 4)] forState:UIControlStateNormal];

    [self setupCustomerColors:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupCustomerColors:) name:BA_LIBRARY_SETTINGS_OBJECT_CHANGED object:nil];
}

-(void)setupCustomerColors:(id)sender
{
    if ([BAUtils instance].iOSMajorVersion >= 7) {
        [self setTintColor:[UIColor whiteColor]];
        
        //iOS 7 magic
        UIView* rootContainerView = [self.subviews firstObject];
        for (UIView* view in rootContainerView.subviews) {
            if([view isKindOfClass:[UITextField class]]) {
                [view setTintColor:[[BALibrarySettings instance] getCustomerColorDark]];
            }

        }
    }
    
    self.backgroundColor = [[BALibrarySettings instance] getCustomerColorNormal];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:BA_LIBRARY_SETTINGS_OBJECT_CHANGED object:nil];
}

//special case for librarian search:
-(void)hideAllBackgrounds
{
    if ([BAUtils instance].iOSMajorVersion >= 7) {
        [self setTintColor:[UIColor whiteColor]];
        
        //iOS 7 magic
        UIView* rootContainerView = [self.subviews firstObject];
        for (UIView* view in rootContainerView.subviews) {
            if([view isKindOfClass:[UITextField class]]) {
                [view setTintColor:[[BALibrarySettings instance] getCustomerColorDark]];
            }
            
        }

        /* was:
        [self setTintColor:[UIColor whiteColor]];
        
        //iOS 7 magic
        UIView* rootContainerView = [self.subviews firstObject];
        for (UIView* view in rootContainerView.subviews) {
            if([view isKindOfClass:[UITextField class]]) {
                [view setTintColor:[UIColor clearColor]];
            }
            
        }
         */
    }
    
    self.backgroundColor = [UIColor clearColor];
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(nil, 1, 30, 8, 0, colorSpace, (CGBitmapInfo) kCGImageAlphaPremultipliedLast);
    CGContextSetRGBFillColor(context, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)0.0, (CGFloat)0.0 );
    CGContextFillRect(context, CGRectMake(0, 0, 1, 30));
    CGImageRef cgimage = CGBitmapContextCreateImage(context);
    UIImage* new_image = [UIImage imageWithCGImage:cgimage];
    //UIImage* empty = [[[UIImage alloc] init] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 1, 1)];
    [self setSearchFieldBackgroundImage:new_image forState:UIControlStateNormal];

}
 

- (void)layoutSubviews {
    [super layoutSubviews];
}

@end
